package com.changan.user.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changan.user.entity.User;
import com.changan.user.response.Result;
import com.changan.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author NieChangan
 * @since 2021-02-21
 */
@Api
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestParam(name = "username",required = true) String username,
                        @RequestParam(name = "password",required = true) String password){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password", SecureUtil.md5(password));
        User user = userService.getOne(wrapper);
        if(user!=null){ //登录成功
            return Result.ok().data("user",user);
        }else{
            return Result.error().message("用户名或者密码错误");
        }
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "增加用户信息",notes = "参数是json格式的user对象")
    public Result addUser(@RequestBody User user){
        boolean b = userService.save(user);
        if(b){
            return Result.ok().message("增加成功");
        }else{
            return Result.error().message("增加失败");
        }
    }
}

